<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<script type="text/javascript">
function process_picking() {
	var dishList = document.getElementsByName('dishList');
	var selectedDishs_str = '';
	for (var i = 0; i < dishList.length; i++) {
		if (dishList[i].checked) {
	   		selectedDishs_str += dishList[i].value + ',';
		}
	}
	
	if (selectedDishs_str != '') {
		selectedDishs_str = selectedDishs_str.substring(0, selectedDishs_str.length - 1);
	}
	window.opener.document.getElementById("dishs_str").value = selectedDishs_str;
	window.close();
}
</script>
</head>
<body>
	<h3>Choose dishes</h3>
	<c:set var="recipeDTO" value="${requestScope.RECIPEDTO}"/>
	<c:set var="dishList" value="${requestScope.DISHLIST}"/>
	
	<c:forEach var="dishItem" items="${dishList}">
		<c:set var="checker" value="false"/>
		<c:forEach var="dishCheckItem" items="${recipeDTO.dishList}">
			<c:if test="${dishItem.id == dishCheckItem.id }">
				<c:set var="checker" value="true"></c:set>
			</c:if>	
		</c:forEach>
		<c:if test="${checker eq 'true'}">
			<input type="checkbox" id="${dishItem.id}" name="dishList" value="${dishItem.id}" checked/>${dishItem.name}<br/>
		</c:if>
		<c:if test="${checker eq 'false'}">
			<input type="checkbox" id="${dishItem.id}" name="dishList" value="${dishItem.id}"/>${dishItem.name}<br/>
		</c:if>
	</c:forEach>
	<input type="button" id="btnProcess" onclick="process_picking()" value="Save"/>
	<input type="button" onclick="top.close()" value="Return back"/>
</body>
</html>